package com.fms.kccc.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.fms.kccc.R;
import com.fms.kccc.util.Common;

public class MenuFragment extends SherlockFragment {
	// Layout
	private TextView tvGoToNew;
	private TextView tvGoToMarked;
	private TextView tvGoToChap;
	private TextView tvExit;
	private CheckBox cbNotification;
	private LinearLayout llNotification;

	private OnSelectMenuListener listener;
	private SharedPreferences preferences;
	private boolean isChecked;

	public static MenuFragment newInstance() {
		MenuFragment fragment = new MenuFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frament_sliding_menu, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void init() {
		preferences = PreferenceManager
				.getDefaultSharedPreferences(getActivity());
		isChecked = preferences
				.getBoolean(Common.Variable.NOTIFY_SETTING, true);
		cbNotification.setChecked(isChecked);
	}

	private void setEventView() {
		tvGoToNew.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onSelectAction(true);
				}
			}
		});
		tvGoToMarked.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onSelectAction(false);
				}
			}
		});

		tvGoToChap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder alert = new AlertDialog.Builder(
						getActivity());
				alert.setMessage("Nhập tập cần chuyển tới");

				// Set an EditText view to get user input
				final EditText input = new EditText(getActivity());
				input.setInputType(InputType.TYPE_CLASS_NUMBER);
				alert.setView(input);

				alert.setPositiveButton("Đồng ý",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								String value = input.getText().toString();
								if (value != null && value.trim().length() > 0) {
									if (listener != null) {
										listener.onGotoChap(Integer
												.parseInt(value));
									}
								}
							}
						});

				alert.setNegativeButton("Hủy",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.cancel();
							}
						});

				alert.show();
			}
		});

		tvExit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onExit();
				}
			}
		});

		cbNotification
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						preferences
								.edit()
								.putBoolean(Common.Variable.NOTIFY_SETTING,
										isChecked).commit();
					}
				});
		llNotification.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isChecked) {
					isChecked = false;
				} else {
					isChecked = true;
				}
				// preferences.edit().putBoolean(
				// Common.Variable.NOTIFY_SETTING, isChecked).commit();
				cbNotification.setChecked(isChecked);
			}
		});
	}

	private void findViewIDs(View v) {
		tvGoToNew = (TextView) v.findViewById(R.id.tv_go_to_new);
		tvGoToMarked = (TextView) v.findViewById(R.id.tv_go_to_marked);
		tvGoToChap = (TextView) v.findViewById(R.id.tv_go_to_chap);
		tvExit = (TextView) v.findViewById(R.id.tv_exit);
		cbNotification = (CheckBox) v.findViewById(R.id.cb_notification);
		llNotification = (LinearLayout) v.findViewById(R.id.ll_notification);
	}

	public void setOnSelectMenuListener(OnSelectMenuListener listener) {
		this.listener = listener;
	}

	public interface OnSelectMenuListener {
		public void onSelectCategory(String category);

		public void onSelectAction(boolean isGotoNew);

		public void onGotoChap(int chap);

		public void onExit();
	}
}
